package cn.alpha.workflow.service.impl;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

	private static String COLUMN="U.ID ID_, U.ACCOUNT FIRST_, U.NICKNAME LAST_, U.EMAIL EMAIL_, U.PASSWORD PWD_";
	private static String TABLE="openoauth.t_sm_user";
	
	@Autowired
	private IdentityService identityService;
	
	public Map<String,User> getUserByIdsMap(Collection<String> ids)
	{
		List<User> users=getUserByIds(ids);
		Map<String,User> idUser=new HashMap<String, User>();
		for(User user:users)
			idUser.put(user.getId(), user);
		return idUser;
	}
	
	public List<User> getUserByIds(Collection<String> ids)
	{
		if(ids==null||ids.size()==0)
			return Collections.emptyList();
		
		StringBuilder getUserByIdsSql=new StringBuilder(200);
		getUserByIdsSql.append("select ").append(COLUMN).append(" from ").append(TABLE)
					.append(" U where U.ID IN (");
		Iterator<String> idIt=ids.iterator();
		String id;
		while(idIt.hasNext())
		{
			id=idIt.next();
			if(id==null)
			{
				idIt.remove();
				continue;
			}
			getUserByIdsSql.append(id);
			if(idIt.hasNext())
				getUserByIdsSql.append(",");
		}
		getUserByIdsSql.append(")");
		return identityService.createNativeUserQuery().sql(getUserByIdsSql.toString()).list();
	}
	
	
}
